﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using Entities;
using System.Globalization;

namespace InsuranceVehicle
{
    public partial class RenewalInsurance : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CustomerID"] == null)
            {
                panelError.Visible = true;
                panelRenewal.Visible = false;
            }
            else
            {
                if (!IsPostBack)
                {
                    panelPayment.Visible = false;
                    addItem();

                    try
                    {
                        setAmount(Convert.ToInt32(cbbPolicyId.SelectedItem.ToString()), Convert.ToInt32(cbbDuration.SelectedItem.ToString()));
                        setPolicyInfomation();
                    }
                    catch (Exception)
                    {
                        lblSuccess.Visible = true;
                        lblSuccess.Text = "You don't have a Policy";
                        btnRenewal.Enabled = false;
                    }
                    
                }
                panelError.Visible = false;
                panelRenewal.Visible = true;
            }

        }

        public void addItem()
        {
            for (int i = 1; i < 10; i++)
            {
                cbbDuration.Items.Add(i.ToString());
            }

            InsurancePolicyAccess ipa = new InsurancePolicyAccess();
            int custID = Convert.ToInt32(Session["CustomerID"].ToString());
            List<InsurancePolicyInfo> list = ipa.getListOfPolicyByCustID(custID);
            foreach (var item in list)
            {
                cbbPolicyId.Items.Add(item.PolicyID.ToString());
            }
        }

        public string setPrice(string price)
        {
            CultureInfo culInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
            NumberFormatInfo numberFormat = culInfo.NumberFormat;
            double temp = Convert.ToDouble(price);
            string newPrice = temp.ToString("c", culInfo);
            return newPrice;
        }

        public void setAmount(int policyID, int duration)
        {

            InsuranceTypeAccess db = new InsuranceTypeAccess();
            InsuranceTypeInfo i = new InsuranceTypeInfo();

            InsurancePolicyAccess ipa = new InsurancePolicyAccess();
            InsurancePolicyInfo ipi = new InsurancePolicyInfo();

            ipi = ipa.getPolicyByID(policyID);
            i = db.getInsuranceByID(ipi.InsuranceID);
            if (i != null)
            {
                duration = Convert.ToInt32(cbbDuration.SelectedItem.ToString());
                lblAmount.Text = setPrice((i.UnitPrice * duration).ToString());
                lblAmountHidden.Text = (i.UnitPrice * duration).ToString();
            }

        }

        protected void rdPayOffline_CheckedChanged(object sender, EventArgs e)
        {
            panelPayment.Visible = false;
        }

        protected void rdPayOnline_CheckedChanged(object sender, EventArgs e)
        {
            panelPayment.Visible = true;
        }

        protected void btnRenewal_Click(object sender, EventArgs e)
        {
            lblErrAccName.Visible = false;
            lblErrAccNum.Visible = false;

            InsurancePolicyAccess ipa = new InsurancePolicyAccess();
            InsurancePolicyInfo ipi = new InsurancePolicyInfo();
            int policyID = Convert.ToInt32(cbbPolicyId.SelectedItem.ToString());
            ipi = ipa.getPolicyByID(policyID);

            int curYear = Convert.ToInt32(ipi.PolicyDuration);
            float curAmount = ipi.InsuredAmount;

            InsurancePolicyInfo UpdateIpi = new InsurancePolicyInfo();
            int newYear = Convert.ToInt32(cbbDuration.SelectedItem.ToString());
            float newAmount = Convert.ToSingle(lblAmountHidden.Text);

            UpdateIpi.PolicyID = policyID;
            UpdateIpi.PolicyDuration = (curYear + newYear).ToString();
            UpdateIpi.InsuredAmount = curAmount + newAmount;
            bool flagName = true;
            bool flagNum = true;
            bool flagService = true;
            if (rdPayOnline.Checked == true)
            {
                if (String.IsNullOrEmpty(txtAccName.Text))
                {
                    lblErrAccName.Visible = true;
                    lblErrAccName.Text = "Please, enter your bank account name";
                    txtAccName.Focus();
                    flagName = false;
                }

                if (String.IsNullOrEmpty(txtAccNum.Text))
                {
                    lblErrAccNum.Visible = true;
                    lblErrAccNum.Text = "Please, enter your bank account number";
                    txtAccNum.Focus();
                    flagNum = false;
                }

                if (flagName && flagNum)
                {
                    PaymentService.Payment service = new PaymentService.Payment();
                    bool status = service.checkPayment(txtAccNum.Text, txtAccName.Text);
                    if (status == true)
                    {
                        UpdateIpi.Status = "paid";
                    }
                    else
                    {
                        lblErrAccNum.Visible = true;
                        lblErrAccNum.Text = "Bank account number is wrong";
                        txtAccNum.Focus();
                        flagService = false;
                    }
                }
            }
            else
            {
                UpdateIpi.Status = "not";
            }

            if (flagName && flagNum && flagService)
            {
                ipa.RenewalInsurance(UpdateIpi);
                lblSuccess.Visible = true;
                lblSuccess.Text = "Policy is renewal successful";
                cbbDuration.Enabled = false;
                cbbPolicyId.Enabled = false;
                btnRenewal.Enabled = false;
                rdPayOffline.Enabled = false;
                rdPayOnline.Enabled = false;
            }

        }

        public void setPolicyInfomation()
        {
            InsurancePolicyAccess ipa = new InsurancePolicyAccess();
            List<InsurancePolicyInfo> list = new List<InsurancePolicyInfo>();
            int policyID = Convert.ToInt32(cbbPolicyId.SelectedItem.ToString());
            list = ipa.getDetailPolicyByID(policyID);

            foreach (var item in list)
            {
                if (item.PolicyID == policyID)
                {
                    lblCustomername.Text = item.CustomerName;
                    lblInsuranceType.Text = item.InsuranceType;
                    lblVehicle.Text = item.PerVehicleControlPlate;
                    lblPolicyDate.Text = item.PolicyDate.ToShortDateString();
                }
            }
        }

        protected void cbbInsuType_SelectedIndexChanged(object sender, EventArgs e)
        {
            setAmount(Convert.ToInt32(cbbPolicyId.SelectedItem.ToString()), Convert.ToInt32(cbbDuration.SelectedItem.ToString()));

            setPolicyInfomation();
        }

        protected void cbbDuration_SelectedIndexChanged(object sender, EventArgs e)
        {
            setAmount(Convert.ToInt32(cbbPolicyId.SelectedItem.ToString()), Convert.ToInt32(cbbDuration.SelectedItem.ToString()));

        }
    }
}